package com.rul.mars.search.controller;

import com.rul.mars.common.api.CommonPage;
import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.EsProduct;
import com.rul.mars.search.service.EsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * 商品搜索服务
 *
 * @author RuL
 */
@Api(tags = "EsProductController", description = "商品搜索服务接口")
@RestController
@RequestMapping("/es/product")
public class
EsProductController {

    @Autowired
    private EsProductService esProductService;

    /**
     * 导入所有数据库中的商品信息到ES
     *
     * @return 导入数据量
     */
    @ApiOperation("导入所有数据库中商品信息到ES")
    @PostMapping("/importAll")
    public CommonResult<Long> importAllList() {
        long count = esProductService.importAll();
        return CommonResult.success(count);
    }

    /**
     * 搜索结果
     *
     * @param keyword  关键字
     * @param pageNum  页面号
     * @param pageSize 页面大小
     * @return 搜索分页信息
     */
    @ApiOperation(value = "简单搜索")
    @GetMapping(value = "/search")
    public CommonResult<CommonPage<EsProduct>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProduct> esProductPage = esProductService.search(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }

}
